package com.seelyn.ddvideo;

import com.seelyn.ddvideo.spider.MovieSubTypeEnum;
import org.jsoup.nodes.Element;
import org.springframework.util.StringUtils;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.Selectable;

import java.util.List;

/**
 * @author slacrey
 * @since 2017/6/11
 */
public class QQVideoListTestProcessor implements PageProcessor {

    //.//*[@id='video_scroll_wrap']/div[2]/div/div[1]/span
//    private String xpathString = "/html/body/div[2]/div[2]/div/div[1]/div[2]/div[2]/div[1]/div/div[2]/div/div[1]/span[@class*='item']";
    private String xpathString = "/html/body/div[@class*='xing_vb']/ul/li/span[@class*='xing_vb4']/a";
    private String typeXpathString = "/html/body/div[@class*='xing_vb']/ul/li/span[@class*='xing_vb5']";
    private String onlineXpathString = "/html/body/div[@class*='xing_vb']/ul/li/span[@class*='xing_vb6']";

    private Site site = Site.me().setDomain("http://ziyuanpian.com")
            .setCharset("UTF-8")
            .setSleepTime(100)
            .setTimeOut(5000)
            .setUserAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31");

    public QQVideoListTestProcessor(Site site) {
        this.site = site;
    }

    @Override
    public void process(Page page) {

        Html body = page.getHtml();
        Selectable spanItems = body.xpath(xpathString);

        if (spanItems == null || spanItems.all().isEmpty()) {
            return;
        }
        List<String> items = spanItems.all();

        Selectable typeSpanItems = body.xpath(typeXpathString);
        if (typeSpanItems == null || typeSpanItems.all().isEmpty()) {
            return;
        }
        List<String> typeItems = spanItems.all();

        Html itemHtml;
        int index = 0;
        for (String item : items) {

            itemHtml = Html.create(item);

            Element aElement = itemHtml.getDocument().body().child(0);
            String link = aElement.attr("href");
            String name = aElement.ownText();

            if (StringUtils.isEmpty(name) || StringUtils.isEmpty(link)) {
                continue;
            }

            itemHtml = Html.create(typeItems.get(index));
            Element typeElement = itemHtml.getDocument().body().child(0);
            if (typeElement != null) {
                String typeText = typeElement.ownText();
                MovieSubTypeEnum typeEnum = MovieSubTypeEnum.getEnum(typeText);
            }
            index++;

            Spider.create(new QQVideoInfoTestProcessor())
                    .addUrl(site.getDomain() + link)
                    .run();

        }

    }

    @Override
    public Site getSite() {
        return site;
    }

}
